package com.manager.action.news;

import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.common.tool.FillBeanUtils;
import org.common.util.TranslateHelper;
import org.wface.web.core.ActionSupport;

import com.manager.service.message.MessageService;
import com.manager.service.news.CategoryService;
import com.web.model.Category;

public class CategoryAction extends ActionSupport {

	private String id;

	public String list() {
		CategoryService categoryService = new CategoryService();
		List<Category> list = categoryService.findList();
		getRequest().setAttribute("categorysList", list);
		return "list";
	}

	public String create() {
		CategoryService categoryService = new CategoryService();
		List<Category> list = categoryService.findList();
		getRequest().setAttribute("categorysList", list);
		return "create";
	}

	public String save() {
		CategoryService categoryService = new CategoryService();
		Category category = new Category();
		// categoryService.
		Map<String, String[]> parameterMap = getRequest().getParameterMap();
		if(getTime(getRequest().getParameter("addtime"))!=null){
			category.setAddtime(getTime(getRequest().getParameter("addtime")));
			}

		new FillBeanUtils(parameterMap).injectBean(category);
		if (category != null && category.getId() > 0) {
			categoryService.update(category);
		} else {
			categoryService.save(category);
		}
		List<Category> list = categoryService.findList();
		getRequest().setAttribute("categorysList", list);
		return "list";
	}

	public String edit() {
		CategoryService categoryService = new CategoryService();
		Category obj = categoryService
				.getById(TranslateHelper.parseInteger(id));
		List<Category> list = categoryService.findList();
		getRequest().setAttribute("categorysList", list);
		getRequest().setAttribute("categorys", obj);
		return "create";
	}

	public String delete() {
		CategoryService categoryService = new CategoryService();
		categoryService.delete(TranslateHelper.parseInteger(id));
		return "!/manager/categorys/list.do";
	}

	public void setId(String id) {
		this.id = id;
	}
	public Timestamp getTime(String str_test){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		  format.setLenient(false);
		  //要转换字符串 str_test
		  Timestamp ts =null;
		  try {
		   ts = new Timestamp(format.parse(str_test).getTime());
		   System.out.println(ts.toString());
		  } catch (ParseException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
		  }
		  return ts;
		  
	}

}
